home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / news / suck-2.6 / suck-2 / suck-2.6.3 / suckutils.c < prev    next >
C/C++ Source or Header  |  1996-03-06  |  5KB  |  188 lines

  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <netdb.h>
  4. #include <sys/stat.h>
  5. #include <errno.h>
  6. #include <dirent.h>
  7. #include <string.h>
  8. #include <signal.h>
  9. #include <limits.h>
  10.  
  11. #include "suck.h"
  12. #include "config.h"
  13. #include "both.h"
  14. #include "suckutils.h"
  15.  
  16. /*------------------------------------------------------------------------*/
  17. /* check if directory exists, if not, try to create it.              */
  18. /* return TRUE if made/exists and can write to it              */
  19. /* -----------------------------------------------------------------------*/
  20. int checkdir(char *dirname) {
  21.     
  22.     struct stat buf;
  23.     int retval = TRUE;
  24.  
  25.     if(stat(dirname, &buf)== 0) {
  26.         /* it exists */
  27.         if(!S_ISDIR(buf.st_mode)) {
  28.             error_log(ERRLOG_REPORT, "%s: Not a directory\n", dirname);
  29.             retval = FALSE;
  30.         }
  31.         else if(access(dirname, W_OK) != 0) {
  32.             error_log(ERRLOG_REPORT, "%s: Write permission denied\n", dirname);
  33.             retval = FALSE;
  34.         }
  35.     }
  36.     else if(errno == ENOENT) {
  37.         /* doesn't exist, make it */
  38.         if(mkdir(dirname, (S_IRUSR | S_IWUSR | S_IXUSR)) != 0) {
  39.             MyPerror(dirname);
  40.             retval = FALSE;
  41.         }
  42.     }
  43.     else {
  44.         /* huh? */
  45.         MyPerror(dirname);
  46.         retval = FALSE;
  47.     } 
  48.     return retval;    
  49. }
  50. /*--------------------------------------------------------------*/
  51. /* if which = FP_SET, store directory in array             */
  52. /*         = FP_GET, retrieve directory and add file name     */
  53. /* retval     FP_SET, if valid dir, the dir, else NULL        */
  54. /*          FP_GET, full path                    */
  55. /*--------------------------------------------------------------*/
  56. char *full_path(int which, int dir, char *fname) {
  57.  
  58.     static char dirs[FP_NR_DIRS][_POSIX_PATH_MAX+1] = { N_TMPDIR, N_DATADIR, N_MSGDIR };
  59.     static char path[_POSIX_PATH_MAX+1];    /* static so area valid after return */
  60.     static char postfix[_POSIX_PATH_MAX+1] = { "\0" };
  61.  
  62.     char *retptr;
  63.     int ok, i;
  64.  
  65.     retptr = NULL;
  66.  
  67.     switch(which) { 
  68.       case FP_GET:
  69.       case FP_GET_NOPOSTFIX:
  70.         /* put the right directory as the first part of the path */
  71.         strcpy(path, dirs[dir]);
  72.         /* if it contains a dit, then change to full path */
  73.         if(path[0] == '.') {
  74.             path[0] = '\0';
  75.             if(getcwd(path, _POSIX_PATH_MAX) == NULL) {
  76.                     MyPerror(path);
  77.                     /* restore current path and pray */
  78.                     /* it will work later */
  79.                     strcpy(path, dirs[dir]);
  80.             }
  81.             else {
  82.                 /* tack on remainder of path */
  83.                 /* the 1 so skip . */
  84.                 strcat(path, &dirs[dir][1]);
  85.             }        
  86.         }
  87.         if(fname != NULL && fname[0] != '\0') {
  88.             i = strlen(path);
  89.             /* if nothing to put on don't do any of this */
  90.             /* put on trailing slant bar if needed */
  91.             if(path[i-1] != '/') {
  92.                 path[i++] = '/';
  93.                 path[i] = '\0';    /* null terminate it */
  94.             }
  95.             /* finally, put on filename */
  96.             strcpy(&path[i], fname);
  97.  
  98.             if(which != FP_GET_NOPOSTFIX && postfix[0] != '\0') {
  99.                 strcat(path, postfix);
  100.             }
  101.         }
  102.         retptr = path;
  103.         break;
  104.       case FP_SET_POSTFIX:
  105.         ok = TRUE;
  106.         strncpy(postfix, fname, sizeof(postfix));
  107.         break;
  108.       case FP_SET:
  109.         ok = TRUE;
  110.         switch(dir) {
  111.           case FP_TMPDIR:
  112.             if(access(fname, W_OK) == -1) {
  113.                 MyPerror(fname);
  114.                 ok = FALSE;
  115.             }
  116.             break;
  117.           case FP_DATADIR:
  118.             if(access(fname, R_OK) == -1) {
  119.                 MyPerror(fname);
  120.                 ok = FALSE;
  121.             }
  122.             break;
  123.           case FP_MSGDIR: /* do_nothing, this is checked elsewhere */
  124.             break;
  125.         }
  126.         if(ok == FALSE) {
  127.             retptr = NULL;
  128.         }
  129.         else {
  130.             strncpy(dirs[dir], fname, sizeof(dirs[0]));
  131.             retptr = dirs[dir];
  132.         }
  133.         break;
  134.     }
  135.     return retptr;
  136. }
  137. #ifdef LOCKFILE
  138. /*--------------------------------------------------------------*/
  139. int do_lockfile(void) {
  140.  
  141.     int retval;
  142.     FILE *f_lock;
  143.     char *lockfile;
  144.     pid_t pid;
  145.  
  146.     retval = RETVAL_OK;
  147.  
  148.     lockfile = full_path(FP_GET, FP_TMPDIR, N_LOCKFILE);
  149.     if((f_lock = fopen(lockfile, "r")) != NULL) {
  150.         /* okay, let's try and see if this sucker is truly alive */
  151.         fscanf(f_lock, "%d", &pid);
  152.         fclose(f_lock);
  153.         if(pid <= 0) {
  154.             error_log(ERRLOG_REPORT, "Lock File %s , Invalid PID, aborting.\n", lockfile);
  155.             retval = RETVAL_ERROR;
  156.         }
  157.         /* this next technique borrowed from pppd, sys-linux.c (ppp2.2.0c) */
  158.         /* if the pid doesn't exist (can't send any signal to it), then try */
  159.         /* to remove the stale PID file so can recreate it. */
  160.         else if(kill(pid, 0) == -1 && errno == ESRCH) {
  161.             /* no pid found */
  162.             if(unlink(lockfile) == 0) {
  163.                 error_log(ERRLOG_REPORT, "Lock File %s , stale PID %d removed.\n", lockfile, pid);
  164.             }
  165.             else {
  166.                 error_log(ERRLOG_REPORT, "Unable to remove Lock File %s , stale PID %d, Aborting.\n", lockfile, pid);
  167.                 retval = RETVAL_ERROR;
  168.             }
  169.         }
  170.         else {
  171.             error_log(ERRLOG_REPORT, "Lock File %s , PID %d exists, aborting.\n", lockfile, pid);
  172.             retval = RETVAL_ERROR;
  173.         }
  174.     }
  175.     if(retval == RETVAL_OK) {
  176.         if((f_lock = fopen(lockfile, "w")) != NULL) { 
  177.             fprintf(f_lock, "%d", getpid());
  178.             fclose(f_lock);
  179.         }
  180.         else {
  181.             error_log(ERRLOG_REPORT, "Unable to create lock file %s\n", lockfile);
  182.             retval = RETVAL_ERROR;
  183.         }
  184.     }
  185.     return retval;
  186. }
  187. #endif
  188.